<?php
namespace app\common\model\mysql;

use think\Model;

class AdminUser extends Model
{

    /**根据后台用户名获取用户信息
     * getStatusAttrUsername
     * @param $username
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getStatusAttrUsername($username)
    {
        if(empty($username)){
            return false;
        }

        $where = [
            'username' => trim($username)
        ];

        $result = AdminUser::where($where)->find();
        return $result;
    }

    /**
     * 根据后台用户登录来修改登录时间登录ip等
     * updateById
     * @param $id
     * @param $data
     * @return bool
     */
    public static function updateById($id,$data)
    {
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }

        $where = [
            'id' => $id
        ];

        $result = AdminUser::where($where)->save($data);
        return $result;
    }

    /**
     * 根据用户名修改密码
     * @param $username
     * @param $data
     * @return bool
     */
    public static function updateByPassword($username,$data)
    {
        if(empty($username) || empty($data) || !is_array($data)){
            return false;
        }

        $where = [
            'username' => $username
        ];

        $result = AdminUser::where($where)->save($data);
        return $result;
    }

    /**
     * root权限组添加后台用户
     * @param $addData
     * @return bool
     */
    public function AddAtuhUser($addData)
    {
        if(empty($addData) || !is_array($addData)){
            return false;
        }

        $result = $this->save($addData);
        return $result;
    }

    /**
     * 查询用户状态码不为1的用户
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function StayActivate($data=[])
    {
        if(empty($data['page'])){
            $page = 1;
        }else{
            $page = $data['page'];
        }
        $result = $this->where('status','<>',1)->paginate(10);
        return $result;
    }

    public function initiated($id)
    {
        $id = intval($id);
        if(empty($id)){
            return false;
        }
        $where = [
            'id' => $id
        ];
        $result = $this->where($where)->save(['status'=>1]);
        return $result;
    }

    public function RemoveAdminUser($id)
    {
       $id = explode(',',$id);
        if(is_array($id)){

            $where = [
                'id' => array_filter($id)
            ];

            $result = $this->where($where)->delete();
            return $result;
        }else{
            $id = intval($id);

            if(empty($id)){
                return false;
            }

            $where = [
                'id' => $id
            ];

            $result = $this->where($where)->delete();
            return $result;
        }
    }

}